# Biblioteca Python + PyGTK - SimpleLogo

import gobject
import gtk
import math
import string

# Globais

DELAY = 100                               # Intervalo de tempo entre duas instrucoes
TIME = DELAY                              # Variavel para controlar o tempo
PENDOWN = True                            # A caneta comeca podendo escrever
TUX_WIDTH = 15                            # Largura da imagem do Tux
TUX_HEIGHT = 15                           # Altura da imagem do Tux
CANVAS_X = 1024                           # Largura do canvas
CANVAS_Y = 768                            # Altura do canvas
TUX_X = CANVAS_X/2                        # Posicao 'x' do Tux. Comeca no centro da tela
TUX_Y = CANVAS_Y/2                        # Posicao 'y' do Tux. Comeca no centro da tela
SET_TUX = True                            # Determina se o Tux deve ou nao aparecer na tela
ANGLE = 270                               # Angulo do Tux. Comeca direcionado para cima
PEN_WIDTH = 1                             # Largura da caneta
COUNT = 0                                 # Contador de movimentos do Tux
window = gtk.Window()                     # Janela do gtk
area = gtk.DrawingArea()                  # Canvas do gtk
bg_color = gtk.gdk.color_parse("#FFFFFF") # Cor de fundo default do canvas (branco)

#------------------------------------------------------------------------------------------

# Funcoes

def move(x,y,tam=-1): # Controla todos os tipos de movimentos do Tux
   global TUX_X
   global TUX_Y
   global PENDOWN
   global PEN_WIDTH
   global ANGLE
   global COUNT
   s = area.get_style()
   gc = s.fg_gc[gtk.STATE_NORMAL]

   if (PENDOWN == True):
      pixmap, mask = gtk.gdk.pixmap_create_from_xpm(area.window, s.bg[gtk.STATE_NORMAL], "img/tux.xpm")
   else:
      pixmap, mask = gtk.gdk.pixmap_create_from_xpm(area.window, s.bg[gtk.STATE_NORMAL], "img/tux_up.xpm")
   
   # Apaga o Tux anterior
   if (SET_TUX):
      blank, mask = gtk.gdk.pixmap_create_from_xpm(area.window, s.bg[gtk.STATE_NORMAL], "img/blank.xpm")
      pangolayout = area.create_pango_layout(str(COUNT))
      area.window.draw_drawable(gc, blank, 0, 0, TUX_X, TUX_Y, TUX_WIDTH, TUX_HEIGHT)
      area.window.draw_layout(gc, TUX_X, TUX_Y, pangolayout)
      COUNT+=1

   if (x == -1): # Significa um goy. Deve-se manter o valor de x
      x = TUX_X
   if (y == -1): # Significa um gox. Deve-se manter o valor de y
      y = TUX_Y
   if (tam != -1): # Significa forward
      x = TUX_X + int(tam*math.cos(ANGLE*(math.pi/180)))
      y = TUX_Y + int(tam*math.sin(ANGLE*(math.pi/180)))
      gc.set_line_attributes(PEN_WIDTH, gtk.gdk.LINE_SOLID, gtk.gdk.CAP_NOT_LAST, gtk.gdk.JOIN_MITER)
      if (PENDOWN):
         area.window.draw_line(gc, TUX_X, TUX_Y, x, y)
   
   if (SET_TUX):
      area.window.draw_drawable(gc, pixmap, 0, 0, x, y, TUX_WIDTH, TUX_HEIGHT)
   
   TUX_X = x
   TUX_Y = y

def go(x, y, tam=-1):
   global TIME
   global DELAY
   gobject.timeout_add(TIME, move, x, y, tam)
   TIME+=DELAY

def gox(x):
   go(x, -1)

def goy(y):
   go(-1, y)

def penup():
   global TIME
   global DELAY
   gobject.timeout_add(TIME, set_penup)
   TIME+=DELAY

def set_penup():
   global TUX_X
   global TUX_Y
   global PENDOWN
   PENDOWN = False
   # Desenha um Tux vermelho
   if (SET_TUX):
      s = area.get_style()
      gc = s.fg_gc[gtk.STATE_NORMAL]
      pixmap, mask = gtk.gdk.pixmap_create_from_xpm(area.window, s.bg[gtk.STATE_NORMAL], "img/tux_up.xpm")
      area.window.draw_drawable(gc, pixmap, 0, 0, TUX_X, TUX_Y, TUX_WIDTH, TUX_HEIGHT)

def pendown():
   global TIME
   global DELAY
   gobject.timeout_add(TIME, set_pendown)
   TIME+=DELAY

def set_pendown():
   global TUX_X
   global TUX_Y
   global PENDOWN
   PENDOWN = True
   # Desenha o Tux normal
   if (SET_TUX):
      s = area.get_style()
      gc = s.fg_gc[gtk.STATE_NORMAL]
      pixmap, mask = gtk.gdk.pixmap_create_from_xpm(area.window, s.bg[gtk.STATE_NORMAL], "img/tux.xpm")
      area.window.draw_drawable(gc, pixmap, 0, 0, TUX_X, TUX_Y, TUX_WIDTH, TUX_HEIGHT)

def forward(tam):
   go(0,0,tam)

def backward(tam):
   turnright(180)
   go(0,0,tam)
   turnright(180)

def turnright(a):
   global TIME
   global DELAY
   gobject.timeout_add(TIME, set_angle, a)
   TIME+=DELAY

def turnleft(a):
   global TIME
   global DELAY
   gobject.timeout_add(TIME, set_angle, -a)
   TIME+=DELAY

def set_angle(a):
   global ANGLE
   ANGLE+=a
   while (ANGLE >= 360):
      ANGLE-=360
   while (ANGLE < 0):
      ANGLE+=360

def reset():
   global TIME
   global DELAY
   gobject.timeout_add(TIME, reset_attr)
   TIME+=DELAY

def reset_attr():
   global TUX_X
   global TUX_Y
   global ANGLE
   global PENDOWN
   global PEN_WIDTH
   global area
   TUX_X=CANVAS_X/2
   TUX_Y=CANVAS_Y/2
   ANGLE=270
   PENDOWN=True
   PEN_WIDTH=1
   area.modify_bg(gtk.STATE_NORMAL, bg_color)

def direction(x):
   global TIME
   global DELAY
   gobject.timeout_add(TIME, set_direction, x)
   TIME+=DELAY

def set_direction(x):
   global ANGLE
   ANGLE=0
   set_angle(x)

def print_string(string):
   global TIME
   global DELAY
   gobject.timeout_add(TIME, draw_string, string)
   TIME+=DELAY

def draw_string(string):
   global area
   global TUX_X
   global TUX_Y
   s = area.get_style()
   gc = s.fg_gc[gtk.STATE_NORMAL]
   pangolayout = area.create_pango_layout(string)
   area.window.draw_layout(gc, TUX_X+TUX_WIDTH, TUX_Y, pangolayout)

def wait(t):
   global TIME
   TIME+=t*1000

def penwidth(x):
   global TIME
   global DELAY
   gobject.timeout_add(TIME, set_penwidth, x)
   TIME+=DELAY

def set_penwidth(x):
   global PEN_WIDTH
   PEN_WIDTH=x
